package com.gateway.config.annotation.dataSource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author Huangkai
 */
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class DataSourceAspect {
    @Pointcut("@annotation(com.gateway.config.annotation.dataSource.DataSource)")
    public void dataSourcePointCut() {
    }
    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Class targetClass = point.getTarget().getClass();
        Method method = signature.getMethod();
        //获取注解类
        DataSource targetDataSource = (DataSource) targetClass.getAnnotation(DataSource.class);
        if (targetDataSource != null) {
            String value = targetDataSource.value();
            //DynamicContextHolder.push(value);
        }
        try {
            return point.proceed();
        } finally {
            //DynamicContextHolder.clearDataSource();
        }
    }
}
